package org.apm.data.bean;

import org.apm.data.util.StringUtil;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Parameter {
    public static final String Day_Pattern = "yyyy-MM-ddHH";
    public static final DateTimeFormatter TF_IN = DateTimeFormat.forPattern("yyyy-MM-ddHH");

    String masterIp;
    String fromInput;
    String toInput;
    String type;
    DateTime from;
    DateTime to;

    //Delete Flags
    boolean multi_type = false;
    boolean all_type = false;
    boolean user_operation = false;

    public Parameter() {
        masterIp = "115.238.29.251";
        from = new DateTime(2017,1,1,0,0,0);
        to = new DateTime();
        fromInput = from.toString(TF_IN);
        toInput = to.toString(TF_IN);
    }

    public void parameter(String[] args) {
        String parameter = "";
        for (String arg : args) {
            if (arg != null) {
                if (arg.startsWith("-")) {
                    if (arg.startsWith("-type")) {
                        type = arg.replace("-type", "").length() > 0 ? arg.replace("-type", "") : type;
                        parameter = "type";
                    } else if (arg.startsWith("-deleteUser") || arg.startsWith("-copyUser") ) {
                        user_operation = true;
                    } else if (arg.startsWith("-mType")) {
                        multi_type = true;
                    } else if (arg.startsWith("-aType")) {
                        all_type = false;
                    } else if (arg.startsWith("-ip")) {
                        masterIp = arg.replace("-ip", "").length() > 0 ? arg.replace("-ip", "") : masterIp;
                        parameter = "masterIp";
                    } else if (arg.startsWith("-from")) {
                        fromInput = arg.replace("-from", "").length() > 0 ? arg.replace("-from", "") : fromInput;
                        parameter = "fromInput";
                    } else if (arg.startsWith("-to")) {
                        toInput = arg.replace("-to", "").length() > 0 ? arg.replace("-to", "") : toInput;
                        parameter = "toInput";
                    } else if (arg.startsWith("-i")) {
                        masterIp = arg.replace("-i", "").length() > 0 ? arg.replace("-i", "") : masterIp;
                        parameter = "masterIp";
                    } else if (arg.startsWith("-f")) {
                        fromInput = arg.replace("-f", "").length() > 0 ? arg.replace("-f", "") : fromInput;
                        parameter = "fromInput";
                    } else if (arg.startsWith("-t")) {
                        toInput = arg.replace("-t", "").length() > 0 ? arg.replace("-t", "") : toInput;
                        parameter = "toInput";
                    }
                } else {
                    setParameter(parameter, arg);
                }
            }
        }

        try {
            from = DateTime.parse(fromInput, TF_IN);
            to = DateTime.parse(toInput, TF_IN);
        } catch (IllegalArgumentException exception) {
            System.out.println("Error: input time format isn't supported. Please follow '" + Day_Pattern + "'");
            System.exit(0);
        }
    }

    public void setParameter(String parameter, String value) {
        switch (parameter) {
            case "type":
                type = value;
                break;
            case "masterIp":
                masterIp = value;
                break;
            case "fromInput":
                fromInput = value;
                break;
            case "toInput":
                toInput = value;
                break;
            default:
                break;
        }
    }

    /** 检查参数是否齐全. */
    public boolean checkParameter(){
        if(StringUtil.areExist(masterIp, type)) {
            return true;
        } else {
            return  false;
        }
    }

    public String getMasterIp() {
        return masterIp;
    }

    public String getType() {
        return type;
    }

    public DateTime getFrom() {
        return from;
    }

    public DateTime getTo() {
        return to;
    }

    public boolean isMulti_type() {
        return multi_type;
    }

    public boolean isAll_type() {
        return all_type;
    }

    public boolean isUser_operation() {
        return user_operation;
    }

    public void setMasterIp(String masterIp) {
        this.masterIp = masterIp;
    }

    @Override
    public String toString() {
        return "Parameter{" +
                "masterIp='" + masterIp + '\'' +
                ", type='" + type + '\'' +
                ", from=" + from +
                ", to=" + to +
                '}';
    }
}
